<?php
Modulator()->registerFunction('api_get_request', 'designerAPICall');

function designerAPICall($request) {
	switch($request) {
		case 'designer-content':
			$query = "SELECT `content_title`, `content_id`, `ct_title` FROM `cn_content` INNER JOIN `cn_content_types` ON `content_type`=`ct_id` ORDER BY `content_title`";
			$results = DB()->getResults($query);
			echo json_encode($results);
			break;
		case 'designer-content-types':
			$query = "SELECT `ct_title`, `ct_plural`, `ct_id` FROM `cn_content_types` ORDER BY `ct_title`";
			$results = DB()->getResults($query);
			echo json_encode($results);
			break;
		case 'designer-tags':
			$query = "SELECT `tag_id`, `tag_title`, `tag_content_type`, `ct_plural` FROM `cn_tags` INNER JOIN `cn_content_types` ON `tag_content_type`=`ct_id` WHERE `tag_type` = 'tag' ORDER BY `tag_title`";
			$results = DB()->getResults($query);
			echo json_encode($results);
			break;
		case 'designer-users':
			$query = "SELECT `user_id`, `user_username`, `user_email` FROM `cn_users` ORDER BY `user_username`";
			$results = DB()->getResults($query);
			echo json_encode($results);
			break;
		case 'designer-content-interactions':
			$query = "SELECT `ci_name`, `ci_id`, `ct_plural` FROM `cn_content_interactions` INNER JOIN `cn_content_types` ON `ci_content_type`=`ct_id` ORDER BY `ci_name`";
			$results = DB()->getResults($query);
			echo json_encode($results);
			break;
		case 'designer-content-relations':
			$query = "SELECT `crt_id`, `crt_name` FROM `cn_content_relation_types` ORDER BY `crt_name`";
			$results = DB()->getResults($query);
			echo json_encode($results);
			break;
		case 'designer-content-fields':
			// Special stuff
			$fields = array();
			$types = DB()->getVarArray("SELECT `ct_id` FROM `cn_content_types`");
			foreach($types as $type) {
				$fields[$type] = DB()->getResults("SELECT * FROM `cn_content_fields` WHERE `cf_content_type`='$type' AND `cf_column` NOT LIKE 'content_%'");
			}
			$fields['defaults'] = DB()->getResults("SELECT * FROM `cn_content_fields` WHERE `cf_content_type`='$type' AND `cf_column` LIKE 'content_%'");
			echo json_encode($fields);
			exit();
			break;
		case 'designer-user-fields':
			// Special stuff
			echo json_encode(DB()->getResults("SELECT * FROM `cn_content_fields` WHERE `cf_content_type`='0'"));
			
			exit();
			break;
		case 'designer-data-types':
			/*
			 * Parses all built-in and module-added data types for their
			 * display parameter fields. Converts them to json for use
			 * with concerto designer
			 */
			if ($handle = opendir(CN_APPLICATION_PATH . 'application/datatypes/')) {
				$returnArray = array();
			    /* This is the correct way to loop over the directory. */
			    while (false !== ($file = readdir($handle))) {
			    	if(!is_dir($file) && !in_array($file, array('DataType.php', 'FileUploadBase.php'))) {
			    		$className = '\\application\\datatypes\\'.str_replace('.php', '', $file);
			    		
			    		// Now we get the parameters and add them to the array
			    		if(property_exists($className, 'displayParams')) {
			    			$returnArray[str_replace('\\application\\datatypes\\', '', $className)] = $className::$displayParams;
			    		}
					}
			    }
			    closedir($handle);
			    
			    // Now get all module data types
			   	$modules = Modulator()->getDataTypeNames();
			   	foreach($modules as $dataType) {
			   		$className = '\\application\\datatypes\\'.$dataType;
			    		
					// Now we get the parameters and add them to the array
					if(property_exists($className, 'displayParams')) {
						$returnArray[str_replace('\\application\\datatypes\\', '', $className)] = $className::$displayParams;
					}
			   	}
			
			}
			
			echo json_encode((object) $returnArray);
			break;
	}
}

?>